package com.miyu.system.controller.sys;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.miyu.common.pojo.book.BookRuleSet;
import com.miyu.system.service.sys.SysBookService;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
/**
 * SaCheckLogin 登录校验 执行此类下方法需要登录
 */
@RestController
@RequestMapping("/system/book")
public class SysBookController {
    @Autowired
    private SysBookService sbs;

    /**
     * 后端查询书籍详情
     * @param param
     * @return
     */
    @RequestMapping("sysQueryBook")
    public JSONArray queryBook(@RequestBody JSONObject param){
       return sbs.queryBook(param);
    }

    /**
     * 后端书籍章节
     * @param param
     * @return
     */
    @RequestMapping("sysQueryBoookDetail")
    public JSONArray sysQueryBoookDetail(@RequestBody JSONObject param){
        return sbs.sysQueryBoookDetail(param);
    }


    /**
     * 所有规则查询
     * @param param
     * @return
     */
    @RequestMapping("queryBookRuleSet")
    public List<BookRuleSet> queryBookRuleSet(@RequestBody JSONObject param){
        return sbs.queryBookRuleSet(param);
    }

    /**
     * 保存规则
     * @param param
     * @return
     */
    @RequestMapping("saveBookRuleSet")
    public JSONObject saveBookRuleSet(@RequestBody JSONObject param){
        return sbs.saveBookRuleSet(param);
    }

    /**
     * 规则删除
     * @param params
     * @return
     */
    @RequestMapping("delBookRuleSet")
    public JSONObject delBookRuleSet(@RequestBody JSONArray params){

        return sbs.delBookRuleSet(params);
    }

    /**
     * 规则取值 不确定返回值类型
     * 以object代替
     * @return
     */
    @RequestMapping("checBookRule")
    public JSONObject checBookRule(@RequestBody JSONObject params){
        return sbs.checkBookRule(params);
    }

    /**
     * 搜索主页查询（爬虫）
     * @param param
     * @return
     */
    @RequestMapping("searchBook")
    public JSONArray searchBook(@RequestBody JSONObject param){
        return sbs.searchBook(param);
    }


    /**
     * 查询书详情（爬虫）
     *
     * @param param 参数
     * @return {@link JSONObject}
     */
    @RequestMapping("queryBookDetail")
    public JSONObject queryBookDetail(@RequestBody JSONObject param){
        return sbs.queryBookDetail(param);
    }

    /**
     * 查询书章节（爬虫）
     *
     * @param param 参数
     * @return {@link JSONObject}
     */
    @RequestMapping("queryBookChapter")
    public JSONObject queryBookChapter(@RequestBody JSONObject param){
        return sbs.queryBookChapter(param);
    }
    /**
     * 明细数据查询（爬虫）
     * @param param
     * @return
     */
    @RequestMapping("querySearchBookDetail")
    public JSONObject querySearchBookDetail(@RequestBody JSONObject param){
        return sbs.querySearchBookDetail(param);
    }

    /**
     * 查询搜索书
     *
     * @param param 参数
     * @return {@link JSONObject}
     */
    @RequestMapping("querySearchBook")
    public JSONObject querySearchBook(@RequestBody JSONObject param){
        return sbs.querySearchBook(param);
    }

    /**
     * 阅读页面（爬虫）
     * @param param
     * @return
     */
    @RequestMapping("queryBookReader")
    public JSONObject queryBookReader(@RequestBody JSONObject param){
        return sbs.queryBookReader(param);
    }


    /**
     * 下载分为 有下载地址 和服务器下载
     * 有下载地址的直接读取流文件下载
     * 无下载地址 直接读取服务网站连接下载

     * @throws Exception
     */

    @RequestMapping("downloadFile")
    public HttpServletResponse downloadFile(HttpServletRequest request,HttpServletResponse response ) throws Exception {
        System.out.println("xiazai.......");
        /*下载链接*/
        String url =  request.getParameter("bookDown");
        System.out.println(url);
        long a= System.currentTimeMillis();

        String bookName = url.substring(url.lastIndexOf("/")+1,url.lastIndexOf("."))+url.substring(url.lastIndexOf("."));
        String ext = url.substring(url.lastIndexOf("."));
        url=new String(url.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8);
        Connection.Response execute = null;
        execute = Jsoup.connect(url).timeout(1000000).maxBodySize(0).ignoreContentType(true).execute();
        response.reset();
        //暂时不乱吗
        response.addHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(bookName, StandardCharsets.UTF_8));
        long b= System.currentTimeMillis();
        System.out.println("shichang>> "+(b-a));

        response.addHeader("ext",ext);
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/octet-stream");
        OutputStream out=  response.getOutputStream();
        out.write(execute.bodyAsBytes());
        out.close();
        return  null;
       // return sbs.downloadFile(param);
    }


    /**
     * 书籍采集
     *
     * @param param 参数
     * @return {@link JSONObject}
     */
    @RequestMapping("collectBook")
    public JSONObject collectBook(@RequestBody JSONObject param){
        return sbs.collectBook(param);
    }

}
